package com.econ.powercloud.ems.controller;

import com.econ.powercloud.common.base.facade.pojo.MsgResult;
import com.econ.powercloud.common.base.facade.pojo.PageResult;
import com.econ.powercloud.ems.aop.UserOperatePower;
import com.econ.powercloud.ems.constant.SysConstant;
import com.econ.powercloud.ems.pojo.*;
import com.econ.powercloud.ems.service.biz.EmsGridService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jwc
 * @date 2022/08/03
 */
@Api(tags = "储能站离并网")
@RestController
@RequestMapping("/api/v2/gird")
@RequiredArgsConstructor
public class EmsGridController extends ApiBaseController {
    private final EmsGridService emsGridService;

    @ApiOperation(value = "获取并离网的PCS模块监控")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "energyStationId", value = "储能站id", dataType = "String"),
    })
    @GetMapping("/pcuMonitor/{energyStationId}")
    public MsgResult<List<EmsGirdPcuMonitorVO>> pcuMonitor(@PathVariable("energyStationId") String energyStationId) {
        return this.successHandler(emsGridService.getPcuMonitor(energyStationId));
    }

    @ApiOperation(value = "获取储能站离并网状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "energyStationId", value = "储能站id", dataType = "String"),
    })
    @GetMapping("/girdStatus/{energyStationId}")
    public MsgResult<EmsGridRecordVO> girdStatus(@PathVariable("energyStationId") String energyStationId) {
        return this.successHandler(emsGridService.getLeastGirdStatusByStationId(energyStationId));
    }

    @ApiOperation(value = "获取并离网流程的状态与最新的组合id")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "recordId", value = "流程id", dataType = "Long"),
    })
    @GetMapping("/statusAndGroupId/{recordId}")
    public MsgResult<EmsGridStatusAndGroupVO> statusAndGroupId(@PathVariable("recordId") Long recordId) {
        return this.successHandler(emsGridService.getStatusAndGroupId(recordId));
    }

    @ApiOperation(value = "获取并离网历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "energyStationId", value = "储能站id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "页码", defaultValue = SysConstant.DEFAULT_PAGENO, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页数量", defaultValue = SysConstant.DEFAULT_PAGESIZE, dataType = "int")
    })
    @GetMapping("/records")
    public MsgResult<PageResult<List<EmsGridRecordVO>>> list(@RequestParam("energyStationId") String energyStationId,
                                                             @RequestParam(value = "pageNo", defaultValue = SysConstant.DEFAULT_PAGENO) Integer pageNo,
                                                             @RequestParam(value = "pageSize", defaultValue = SysConstant.DEFAULT_PAGESIZE) Integer pageSize) {
        return successHandler(emsGridService.listPage(energyStationId, pageNo, pageSize));
    }

    @ApiOperation(value = "获取操作记录的详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "recordId", value = "操作记录的id", dataType = "Long")
    })
    @GetMapping("/record/{recordId}")
    public MsgResult<EmsGridRecordDetailsVO> record(@PathVariable("recordId") Long recordId) {
        return successHandler(emsGridService.getRecordDetails(recordId));
    }

    @ApiOperation(value = "获取组合记录的详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "moduleRecordId", value = "组合记录的id", dataType = "Long")
    })
    @GetMapping("/recordGroup/{recordGroupId}")
    public MsgResult<EmsGridRecordGroupVO> recordGroup(@PathVariable("recordGroupId") Long recordGroupId) {
        return this.successHandler(emsGridService.getRecordGroupDetailById(recordGroupId));
    }

    @UserOperatePower
    @ApiOperation(value = "储能站离网")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "energyStationId", value = "储能站id", dataType = "String"),
    })
    @PostMapping(value = "/off/{energyStationId}")
    public MsgResult<Long> off(@PathVariable("energyStationId") String energyStationId) {
        return this.successHandler(emsGridService.offGrid(energyStationId));
    }

    @UserOperatePower
    @ApiOperation(value = "储能站并网")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "energyStationId", value = "储能站id", dataType = "String"),
    })
    @PostMapping(value = "/on/{energyStationId}")
    public MsgResult<Long> on(@PathVariable("energyStationId") String energyStationId) {
        return this.successHandler(emsGridService.onGrid(energyStationId));
    }

    @UserOperatePower
    @ApiOperation(value = "手动终止当前离网或并网操作")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "recordId", value = "操作记录id", dataType = "Long"),
    })
    @PostMapping(value = "/stop/{recordId}")
    public MsgResult stop(@PathVariable("recordId") Long recordId){
        emsGridService.stop(recordId);
        return this.successHandler(true);
    }

    @UserOperatePower
    @ApiOperation(value = "继续执行组合")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "recordGroupId", value = "组合记录id", dataType = "Long"),
    })
    @PostMapping(value = "/confirmGroup")
    public MsgResult confirmGroup(@RequestParam("recordGroupId") Long recordGroupId) {
        emsGridService.confirmGroup(recordGroupId);
        return successHandler(true);
    }

    @ApiOperation(value = "根据步骤记录id执行步骤")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "recordStepId", value = "步骤记录id", dataType = "Long"),
    })
    @PostMapping(value = "/executeStep")
    public MsgResult executeStep(@RequestParam("recordStepId") Long recordStepId) {
        emsGridService.executeStepById(recordStepId);
        return successHandler(true);
    }
}
